package com.zlp.boot1.servlet;

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

/**
 * @Author: zhulaiping
 * @Date: 2021/10/31 14:06
 */
@Component
@WebServlet(value = "/servlet1",initParams =
        {@WebInitParam(name = "name",value = "zlp"),@WebInitParam(name = "age",value = "18")})
public class ServletDemo1 extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");

        PrintWriter out = resp.getWriter();
        out.println("<html><head><title>RequestInfo</title></head>");
        out.println("<body>");
        out.println("<br>LocalAddr: "+req.getLocalAddr());
        out.println("<br>LocalName: "+req.getLocalName());
        out.println("<br>LocalPort: "+req.getLocalPort());
        out.println("<br>LocalProtocol: "+req.getProtocol());

        out.println("<br>RemoteAddr: "+req.getRemoteAddr());
        out.println("<br>RemoteHost: "+req.getRemoteHost());
        out.println("<br>RemotePort: "+req.getRemotePort());
        out.println("<br>:Method :"+req.getMethod());
        out.println("<br>URI: "+req.getRequestURI());
        out.println("<br>ContextPath : "+req.getContextPath());
        out.println("<br>QueryString: "+req.getQueryString());

        out.println("<br>******打印http请求头******");
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String element = headerNames.nextElement();
            out.println("<br>"+element+": "+req.getHeader(element));
        }
        out.println("<br>******打印http请求头结束******");

        out.println("</body></html>");

        //获取改servlet的config对象
        ServletConfig config = getServletConfig();
        Enumeration<String> names = config.getInitParameterNames();
        while (names.hasMoreElements()){
            String s = names.nextElement();
            System.out.println(s+": "+config.getInitParameter(s));
        }
        System.out.println(names.hasMoreElements());
        System.out.println(config.getServletName());
        ServletContext context = config.getServletContext();

    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        doGet(req,resp);
    }
}
